package util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.ListResourceBundle;
import java.util.Locale;
import java.util.Properties;
import java.util.ResourceBundle;

public class UserList{
	
	protected static Properties userList;
	protected static Properties infoList;
	private FileWriter userListWriter;
	private FileWriter userInfoWriter;

	public UserList() throws FileNotFoundException, IOException{
		userList = new Properties();
		infoList = new Properties();
		userList.load(new FileInputStream("src/util/users"));
		infoList.load(new FileInputStream("src/util/info"));
	}
	
	public boolean checkLogin(userObject user){
		if(userList.containsKey(user.getUsername()))
			if(userList.getProperty(user.getUsername()).equals(user.getPassword()))
				return true;
			else
				return false;
		else
			return false;
	}
	
	public boolean writeUser(userObject user){
		
		if(!userList.containsKey(user.getUsername())){	
			try{
					userListWriter = new FileWriter(new File("src/util/users"), true);
					userListWriter.write(user.getUsername() + "=" + user.getPassword() + "\n");
					userListWriter.close();
					userInfoWriter = new FileWriter(new File("src/util/info"), true);
					userInfoWriter.write(user.getUsername() + ":" +	user.getFirst() + ":" + user.getLast() + 
							":" + user.getPhone() + "\n");
					userInfoWriter.close();
			}
			catch(IOException e){
				System.out.println(e);
			}
			return true;
		}
		else
			return false;
	}
	
}
